class Solution:
    def generateParenthesis(self, n):
        res = []
        def generate(p,l,r):
            if l:
                generate(p+"(",l-1,r)
            if l<r:
                generate(p+")",l,r-1)
            if not r:
                res.append(p)
            return res
        return generate("",n,n)

print Solution().generateParenthesis(3)
